package com.penjin.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.penjin.android.R;
import com.penjin.android.activity.secretary.PushDetailsWebView;
import com.penjin.android.activity.secretary.ZhangLiangDetailsActivity;
import com.penjin.android.domain.News;
import com.penjin.android.http.HttpImage;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.zip.Inflater;

/**
 * Created by lxl on 2016/4/7.
 */
public class ZhangLiangListAdapter extends BaseAdapter {

    private List<News> datas = new ArrayList<News>() ;

    private Context context ;

    private String next ;

    public ZhangLiangListAdapter(Context context , List<News> datas ,String next ){

        this.next = next ;
        this.context = context ;
        this.datas = datas ;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder = new ViewHolder() ;

        View view = LayoutInflater.from(context).inflate(R.layout.item_zhangliang_list,null) ;

        viewHolder.newImage1 = (ImageView) view.findViewById(R.id.zhangliang_news1_image);
        viewHolder.newImage2 = (ImageView) view.findViewById(R.id.zhangliang_news2_image);
        viewHolder.newImage3 = (ImageView) view.findViewById(R.id.zhangliang_news3_image);
        viewHolder.newImage4 = (ImageView) view.findViewById(R.id.zhangliang_news4_image);
        viewHolder.newImage5 = (ImageView) view.findViewById(R.id.zhangliang_news5_image);

        viewHolder.newTitile1 = (TextView) view.findViewById(R.id.zhangliang_news1_title);
        viewHolder.newTitile2 = (TextView) view.findViewById(R.id.zhangliang_news2_title);
        viewHolder.newTitile3 = (TextView) view.findViewById(R.id.zhangliang_news3_title);
        viewHolder.newTitile4 = (TextView) view.findViewById(R.id.zhangliang_news4_title);
        viewHolder.newTitile5 = (TextView) view.findViewById(R.id.zhangliang_news5_title);

        final News news = (News) getItem(position) ;

        switch (news.getCount()){

            case 5:
                viewHolder.newTitile5.setText(news.getItem(4).getTitle());

                HttpImage httpImage5 = new HttpImage(news.getItem(4).getImage()) ;

                httpImage5.loadImage(new HttpImage.ImageDownLoadCallback() {
                    @Override
                    public void getDrawable(Drawable drawable) {

                        viewHolder.newImage5.setImageDrawable(drawable);

                    }
                });

                viewHolder.news5 = view.findViewById(R.id.item_zhangliang_news5) ;
                viewHolder.news5.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(context, PushDetailsWebView.class) ;
                        intent.putExtra("id",news.getItem(4).getId()) ;
                        intent.putExtra("post",next) ;
                        context.startActivity(intent);

                    }
                });

            case 4:
                viewHolder.newTitile4.setText(news.getItem(3).getTitle());

                HttpImage httpImage4 = new HttpImage(news.getItem(3).getImage()) ;

                httpImage4.loadImage(new HttpImage.ImageDownLoadCallback() {
                    @Override
                    public void getDrawable(Drawable drawable) {

                        viewHolder.newImage4.setImageDrawable(drawable);

                    }
                });

                viewHolder.news4 = view.findViewById(R.id.item_zhangliang_news4) ;
                viewHolder.news4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, PushDetailsWebView.class) ;
                    intent.putExtra("id",news.getItem(3).getId()) ;
                    intent.putExtra("post",next) ;
                    context.startActivity(intent);

                }
            });
            case 3:
                viewHolder.newTitile3.setText(news.getItem(2).getTitle());

                HttpImage httpImage3 = new HttpImage(news.getItem(2).getImage()) ;

                httpImage3.loadImage(new HttpImage.ImageDownLoadCallback() {
                    @Override
                    public void getDrawable(Drawable drawable) {

                        viewHolder.newImage3.setImageDrawable(drawable);

                    }
                });

                viewHolder.news3 = view.findViewById(R.id.item_zhangliang_news3) ;
                viewHolder.news3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(context, PushDetailsWebView.class) ;
                        intent.putExtra("id",news.getItem(2).getId()) ;
                        intent.putExtra("post",next) ;
                        context.startActivity(intent);

                    }
                });

            case 2:
                viewHolder.newTitile2.setText(news.getItem(1).getTitle());

                HttpImage httpImage2 = new HttpImage(news.getItem(1).getImage()) ;

                httpImage2.loadImage(new HttpImage.ImageDownLoadCallback() {
                    @Override
                    public void getDrawable(Drawable drawable) {

                        viewHolder.newImage2.setImageDrawable(drawable);

                    }
                });

                viewHolder.news2 = view.findViewById(R.id.item_zhangliang_news2) ;
                viewHolder.news2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(context, PushDetailsWebView.class) ;
                        intent.putExtra("id",news.getItem(1).getId()) ;
                        intent.putExtra("post",next) ;
                        context.startActivity(intent);

                    }
                });

            case 1:
                viewHolder.newTitile1.setText(news.getItem(0).getTitle());

                HttpImage httpImage1 = new HttpImage(news.getItem(0).getImage()) ;

                httpImage1.loadImage(new HttpImage.ImageDownLoadCallback() {
                    @Override
                    public void getDrawable(Drawable drawable) {

                        viewHolder.newImage1.setImageDrawable(drawable);

                    }
                });


                viewHolder.news1 = view.findViewById(R.id.item_zhangliang_news1) ;
                viewHolder.news1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(context, PushDetailsWebView.class) ;
                        intent.putExtra("id",news.getItem(0).getId()) ;
                        intent.putExtra("post",next) ;
                        context.startActivity(intent);

                    }
                });

                break;
            default:
                break;


        }

        return view;
    }


    private static class  ViewHolder{

        TextView  newTitile1 ;
        TextView  newTitile2 ;
        TextView  newTitile3 ;
        TextView  newTitile4 ;
        TextView  newTitile5 ;

        ImageView newImage1 ;
        ImageView newImage2 ;
        ImageView newImage3 ;
        ImageView newImage4 ;
        ImageView newImage5 ;

        View news1 ;
        View news2 ;
        View news3 ;
        View news4 ;
        View news5 ;



    }

}
